﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Service;
using LIMS.Entities;
using Castle.Core.Logging;

namespace LIMS.Shared
{
    public class UserRegistrationPagePresenter : BasePresenter<IUserRegistrationPageView>
    {
        private IUserRegistrationPageView view;
        private ILogger logger;
        private IUserManagementService  userService;
        public UserRegistrationPagePresenter(IUserRegistrationPageView view,ILogger oLogger, IUserManagementService myUserService)
            : base(view)
        {
            this.view = view;
            this.logger = oLogger;
            this.userService = myUserService;
        }

        /// <summary>
        /// Checks if a user name is duplicate in database
        /// </summary>
        /// <param name="sUserName"></param>
        /// <returns></returns>
        public bool IsDuplicateUserName(string sUserName)
        {
            if (userService.GetUserByUserID(sUserName) != null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Checks if a user name is duplicate in database
        /// </summary>
        /// <param name="sUserName"></param>
        /// <returns></returns>
        public bool IsDuplicateEmailAddress(string sEmail)
        {
            if (userService.GetUserByEmailAddress(sEmail) != null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Saves current user..
        /// </summary>
        /// <param name="newUser"></param>
        /// <returns></returns>
        public bool Save(User newUser)
        {
            //Try saving user
            if (userService.Save(newUser))
            {
                //Try sending registration Email
                if(!userService.SendUserRegistrationEmails(newUser))
                {
                    logger.Error("User :" + newUser.UserName  + " created successfully but could not send mail to administrator");
                }

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
